/*
 * Nomin.c
 *
 *  Created on: Jan 28, 2012
 *      Author: Georgios
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <conio.h>

#include "Nomin.h"
#include "StackPimitiveDataTypes.h"
#include "BinTree.h"
#include "MyStrings.h"
#include "HashNomi.h"

#define _IN_TYPE_ unsigned char

void callByValue(int, int);
void callByReference(int *, int *);
int numberOnesInBinary(_IN_TYPE_ number);
int numberOnesInBinaryFaster(_IN_TYPE_ number);
_IN_TYPE_ reverseBits(_IN_TYPE_ bits);

int main()
{
	int n = numberOnesInBinaryFaster(1);
	n=n+1;
	n =  reverseBits(0);
	n =  reverseBits(1);
	n =  reverseBits(2);
	n =  reverseBits(3);
	n =  reverseBits(4);
	n =  reverseBits(5);
	n =  reverseBits(6);
	n = numberOnesInBinary(255);
	n = numberOnesInBinary(254);
	n = numberOnesInBinaryFaster(255);
	n = numberOnesInBinaryFaster(254);
	n = numberOnesInBinaryFaster(0);
    int len = strlen("hello?  ");
    char * str = malloc(sizeof(char) * len);
    strcpy(str, "hello?  ");
    printf("%s\n", str);
    int hValue = 0;
    hValue = hValue + 1;
    hValue = hashCodeOfAString(str);
    hValue = hashCodeOfAString("hello?");
    hValue = hashCodeOfAString("helol?");

    hValue = hashInt(111);

    mytrim(str);
    printf("%s\n", str);
    strcpy(str, "        ");
    strcpy(str, mytrim(str));
    if (str != NULL) {
    	printf("%s\n", str);
    }
    strcpy(str, mytrim("       1"));
    printf("%s\n", str);
    strcpy(str, mytrim("       2"));
    printf("%s\n", str);
    free(str);
    str = NULL;

    int n1, n2, n3, n4, n5, n6, n7, n8, n9;
    char s1[] = "Hello G";
    char *s2 = s1;
    n1 = sizeof("He");
    n1 = sizeof(s1);
    n2 = sizeof(s2);
    n3 = sizeof(&s1);
    n4 = sizeof(char);
    n5 = sizeof(int);
    n6 = sizeof(short);
    n7 = sizeof(long);
    n8 = sizeof(float);
    n9 = sizeof(double);
    n3 = sizeof(n2/n3);

    n1 = n9+n8+n7+n5+n4+n1+n6;

    len = strlen("hello Georgios how are you?");
    char * str1 = malloc(sizeof(char) * len);
    strcpy(str1, "hello Georgios how are you?");
    str1 = mytrim(str1);
    str1 = mytrim(NULL);
    free(str1);
    str1=NULL;

    printf("\n");
    printf("\n----------------------------------------------\n");
    printf("\nBinary Trees\n");
    printf("\n----------------------------------------------\n");

    node_t * nd = NULL;

    nd = Insert(nd, 4);
    nd = Insert(nd, 2);
    nd = Insert(nd, 5);
    nd = Insert(nd, 1);
    nd = Insert(nd, 3);
    printf("\nThe size of the binary tree is :  %d\n", Size(nd));

    printf("\nThe Maximum depth of the binary tree is :  %d\n", MaxDepth(nd));

    printf("\nThe Minimum value of the binary tree is :  %d\n", MinValue(nd));

    printf("\nPrint the binary tree\n");
    PrintTree(nd);


    StackChar();

    StackInt(50);

    //Test BLock variables
    {
		/* Begin local block for function main() */
		int test_var = 10;
		printf("Test variable before the if statement: %d\n", test_var);
		if (test_var > 5)
		{
			 /* Begin local block for "if" statement */
			 int test_var = 5;
			 printf("Test variable within the if statement: %d\n",
					test_var);
			 {
				  /* Begin independent local block (not tied to
					 any function or keyword) */
				  int test_var = 0;
				  printf(
				  "Test variable within the independent local block:%d\n",
				  test_var);
			 }
			 printf("Test variable within the if statement: %d\n",
					test_var);
			 /* End independent local block */
		}
		/* End local block for "if" statement */
		printf("Test variable after the if statement: %d\n", test_var);
    }

    printf("\n");
    printf("\n----------------------------------------------\n");
    printf("\nCall by value and Call by reference\n");
    printf("\n----------------------------------------------\n");

    int x=10, y=20;

    printf("\nValue of x = %d and y = %d.\n", x,y);


    callByValue(x, y);
    printf("\nCall By Value function call...\n");
    printf("Value of x = %d and y = %d.\n", x,y);

    callByReference(&x, &y);
    printf("\nCall By Reference function call...\n");
    printf("Value of x = %d and y = %d.\n", x,y);

    return 0;
}

int numberOnesInBinary(_IN_TYPE_ number) {
	int numOnes = 0;
	while (number != 0) {
		if ((number & 1) == 1) {
			numOnes++;
		}
		number = number >> 1;
	}
	return numOnes;
}

int numberOnesInBinaryFaster(_IN_TYPE_ number) {
	int numOnes = 0;
	while (number != 0) {
		number = number & (number -1);
		numOnes++;
	}
	return numOnes;
}

_IN_TYPE_ reverseBits(_IN_TYPE_ bits) {

	bits = ((bits>>1) & 0x55) | ((bits<<1) & 0xAA);
	bits = ((bits>>2) & 0x33) | ((bits<<2) & 0xCC);
	bits = (bits>>4) | (bits<<4) ; //NB: code not tested
	return bits;
}

void callByValue(int x, int y)
{
    int temp;
    temp = x;
    x = y;
    y = temp;
}

void callByReference(int *x, int *y)
{
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
